﻿
//***********************************************************************
// * Module:   TbBstOnekeyAlarmHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/4
// * Purpose: Definition of the Class TbBstOnekeyAlarmHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstOnekeyAlarmHandler 的摘要说明
    /// </summary>
    public class TbBstOnekeyAlarmHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {

                TbBstOnekeyAlarm mTbBstOnekeyAlarm = new TbBstOnekeyAlarm();
                mTbBstOnekeyAlarm.OnekeyAlarmPkid = Guid.NewGuid().ToString(); //主键
                mTbBstOnekeyAlarm.UserId = context.Request.Form["texUserId"]; //用户编号  关联用户信息
                mTbBstOnekeyAlarm.PhoneNo = context.Request.Form["texPhoneNo"]; //用户手机号
                mTbBstOnekeyAlarm.GpsX = context.Request.Form["texGpsX"]; //信息位置  xxx.xxxx
                mTbBstOnekeyAlarm.GpsY = context.Request.Form["texGpsY"]; //信息位置  yyy.yyyy
                mTbBstOnekeyAlarm.AlarmDate = DateTime.Parse(context.Request.Form["texAlarmDate"]); //报警时间
                mTbBstOnekeyAlarm.ProcessResult = context.Request.Form["texProcessResult"]; //处理结果
                mTbBstOnekeyAlarm.ProcessDate = DateTime.Parse(context.Request.Form["texProcessDate"]); //处理时间

                mResultModel = TbBstOnekeyAlarmBLL.Insert(mTbBstOnekeyAlarm, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstOnekeyAlarm mTbBstOnekeyAlarm = new TbBstOnekeyAlarm();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstOnekeyAlarmBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstOnekeyAlarm mTbBstOnekeyAlarm = new TbBstOnekeyAlarm();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstOnekeyAlarm = TbBstOnekeyAlarmBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstOnekeyAlarm != null)
                {
                    //mTbBstOnekeyAlarm.PhoneNo = context.Request.Form["texPhoneNo"]; //用户手机号
                    //mTbBstOnekeyAlarm.GpsX = context.Request.Form["texGpsX"]; //信息位置  xxx.xxxx
                    //mTbBstOnekeyAlarm.GpsY = context.Request.Form["texGpsY"]; //信息位置  yyy.yyyy
                    //mTbBstOnekeyAlarm.AlarmDate = DateTime.Parse(context.Request.Form["texAlarmDate"]); //报警时间
                    mTbBstOnekeyAlarm.ProcessResult = context.Request.Form["texProcessResult"]; //处理结果
                    mTbBstOnekeyAlarm.ProcessDate = DateTime.Now; //处理时间
                    mTbBstOnekeyAlarm.ProcessStatus = "1";


                    mResultModel = TbBstOnekeyAlarmBLL.Update(mTbBstOnekeyAlarm, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstOnekeyAlarm mTbBstOnekeyAlarm = new TbBstOnekeyAlarm(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstOnekeyAlarm = TbBstOnekeyAlarmBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstOnekeyAlarm != null)
                    {
                        mTbBstOnekeyAlarm.ProcessStatus = mTbBstOnekeyAlarm.ProcessStatus == "0" ? "未处理" : "已处理";
                        if (mTbBstOnekeyAlarm.ProcessDate.ToString("yyyy-MM-dd") == "0001-01-01")
                        {
                            mTbBstOnekeyAlarm.ProcessDateStr = "未处理";
                        }
                        else
                        {
                            mTbBstOnekeyAlarm.ProcessDateStr = mTbBstOnekeyAlarm.ProcessDate.ToString();
                        }
                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(mTbBstOnekeyAlarm, dtConverter);

                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string phoneNo = context.Request.Form["texPhoneNo"]; //用户手机号
            string status = context.Request.Form["selStatus"]; //处理状态
            string timeStart = context.Request.Form["texTimeStart"]; //开始时间
            string timeEnd = context.Request.Form["texTimeEnd"]; //结束时间
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstOnekeyAlarm = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList sqllist = new SqlConditionList();
            string sqlWhere = " deltag=0 ";
            if (!string.IsNullOrEmpty(phoneNo))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstOnekeyAlarmConst.Columns.PhoneNo, '%' + phoneNo + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(status))
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstOnekeyAlarmConst.Columns.ProcessStatus, status), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(timeStart))
            {
                DateTime dtstart = new DateTime();
                if (DateTime.TryParse(timeStart, out dtstart))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstOnekeyAlarmConst.Columns.AlarmDate, dtstart), SqlLogicOper.AND_LOGIC);
                }
            }
            if (!string.IsNullOrEmpty(timeEnd))
            {
                DateTime dtend = new DateTime();
                if (DateTime.TryParse(timeEnd, out dtend))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstOnekeyAlarmConst.Columns.AlarmDate, dtend), SqlLogicOper.AND_LOGIC);
                }
            }
            if (string.IsNullOrEmpty(sqllist.ToSql()))
            {
                sqlWhere = "where " + sqlWhere;
            }
            else
            {
                sqlWhere = sqllist.ToSql() + " and " + sqlWhere;
            }
            dtTbBstOnekeyAlarm = Q.GetRecordByPage("ONEKEY_ALARM_PKID,PHONE_NO,GPSX,GPSY,ALARM_DATE,PROCESS_STATUS", sqlWhere, " ALARM_DATE ", intPageSize, intPageIndex, TbBstOnekeyAlarmConst.TableName, TbBstOnekeyAlarmConst.FieldKey, "desc").Tables[0];
            if (dtTbBstOnekeyAlarm != null && dtTbBstOnekeyAlarm.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstOnekeyAlarm.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstOnekeyAlarm.Columns[i].Caption;
                    list.Add(hm);
                }
                for (int i = 0; i < dtTbBstOnekeyAlarm.Rows.Count; i++)
                {
                    dtTbBstOnekeyAlarm.Rows[i][TbBstOnekeyAlarmConst.Columns.ProcessStatus] = dtTbBstOnekeyAlarm.Rows[i][TbBstOnekeyAlarmConst.Columns.ProcessStatus].ToString() == "0" ? "未处理" : "已处理";
                }
                tm.Header = list;
                tm.data = dtTbBstOnekeyAlarm;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);
            context.Response.Write(json);
        }
        #endregion

    }
}


